﻿using BLL;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PropertyAppraisal
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button1_Click(object sender, EventArgs e)
        {

            UsersBll bll = new UsersBll();
            //非空判断
            if (userid.Text != "" || pwd.Text != "")
            {
                string name = (userid.Text).ToString();
                string userpwd = (pwd.Text).ToString();
                List<SysUser> UserList = bll.UsersLogin(name, userpwd);
                //验证码验证
                if (vdcode.Text.ToUpper() != Session["CheckCode"].ToString())
                {
                    Response.Write("<script>alert('验证码出错')</script>");
                }
                else
                {
                    if (UserList.Count != 0)
                    {

                        var list = UserList.FirstOrDefault();
                        Session["id"] = list.Roles_id;
                        Session["UserName"] = list.users_Name;
                        Session.Timeout = 30;
                        // 提供对票证的属性和值的访问，这些票证用于 Forms 身份验证对用户进行标识。
                        FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(list.users_Name, false, 1);
                        //加密票据
                        string ticString = FormsAuthentication.Encrypt(ticket);
                        //输出到客户端
                        FormsAuthentication.SetAuthCookie(ticString, false);

                        Response.Write("<script>alert('登录成功');location.href='Index.aspx'</script>");
                    }
                    else
                    {
                        Response.Write("<script>alert('账户或密码错误，登录失败')</script>");
                    }
                }

            }
            else
            {
                Response.Write("<script>alert('账户或密码不能为空')</script>");
            }

        }
    }
}